package com.health.viewscontrols;

import java.util.ArrayList;
import java.util.List;

import com.health.R;
import com.health.adapter.FragmentAdapter;
import com.health.db.FDbManager;
import com.health.fragment.person.CoverFragment;
import com.health.fragment.person.DetailInfoFragment;
import com.health.fragment.person.HealthyFragment;
import com.health.tablebean.ARCHIVE_BASEINFO;
import com.health.tools.SPUtil;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

/**
 * 个人档案
 * 
 * @author nova
 *
 */
public class PersonalFileActivity extends NovaFragmentActivity implements OnClickListener {

	private ViewPager viewPager;
	private ArrayList<RadioButton> radioButtons;
	private ArrayList<Fragment> fragments;
	private Button saveBtn;
	private Button backBtn;
	private FragmentAdapter adapter;
	private TextView title;
	private Fragment detailFragment;
	private FDbManager fdbmanger = NovaApplication.fdbmanger;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		// getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
		// WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.personal_file);
		initView();
		addListener();
	}

	private void initView() {
		title = (TextView) findViewById(R.id.title);

		viewPager = (ViewPager) findViewById(R.id.viewpager);
		RadioButton personal_file_cover = (RadioButton) findViewById(R.id.personal_file_cover);
		RadioButton personal_file_detail = (RadioButton) findViewById(R.id.personal_file_detail);
		RadioButton personal_file_health = (RadioButton) findViewById(R.id.personal_file_health);
		LinearLayout ll_finish = (LinearLayout)findViewById(R.id.ll_finish);
		TextView tv_title = (TextView)findViewById(R.id.tv_title);
		Button bt_save = (Button)findViewById(R.id.bt_save);
		ll_finish.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				finish();
			}
		});
		tv_title.setText("居民健康档案");

		radioButtons = new ArrayList<RadioButton>();
		radioButtons.add(personal_file_cover);
		radioButtons.add(personal_file_detail);
		radioButtons.add(personal_file_health);

		personal_file_cover.setOnClickListener(this);
		personal_file_detail.setOnClickListener(this);
		personal_file_health.setOnClickListener(this);
		bt_save.setOnClickListener(this);

		Fragment coverFragment = new CoverFragment();
		detailFragment = new DetailInfoFragment();
		Fragment healthyFragment = new HealthyFragment();

		fragments = new ArrayList<Fragment>();
		fragments.add(coverFragment);
		fragments.add(detailFragment);
		fragments.add(healthyFragment);

		adapter = new FragmentAdapter(getSupportFragmentManager(), fragments);
		viewPager.setAdapter(adapter);

		View head = findViewById(R.id.person_head);
		saveBtn = (Button) head.findViewById(R.id.btn_up);
		backBtn = (Button) head.findViewById(R.id.btn_back);
		saveBtn.setOnClickListener(this);
		backBtn.setOnClickListener(this);
		title.setText("个人档案  —— 封面信息");
		setChecked(0);
	}

	private void addListener() {

		viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

			}

			@Override
			public void onPageSelected(int position) {
				radioButtons.get(position).setChecked(true);
				setChecked(position);
			}

			@Override
			public void onPageScrollStateChanged(int state) {

			}
		});
	}

	@Override
	public void onClick(View v) {

		int id = v.getId();
		if (id == R.id.personal_file_cover) {
			viewPager.setCurrentItem(0);
			setChecked(0);
		} else if (id == R.id.personal_file_detail) {
			viewPager.setCurrentItem(1);
			setChecked(1);
		} else if (id == R.id.personal_file_health) {
			viewPager.setCurrentItem(2);
			setChecked(2);
		} else if (id == R.id.bt_save) {
			onFragmentSave currentFragment = (onFragmentSave) adapter.getCurrentFragment();
			ARCHIVE_BASEINFO userInfo = NovaApplication.getUserInfo();
			String strWhere = "IDCARD LIKE '" + NovaApplication.getUserInfo().getIDCARD() + "'";
			List<ARCHIVE_BASEINFO> infos = fdbmanger.findAllByWhere(ARCHIVE_BASEINFO.class, strWhere);
			if (infos.size() > 0) {
				// fdbmanger.delete(infos.get(0));
				userInfo.setIS_UPDATE("Y");
				fdbmanger.updata(userInfo, strWhere);
				userInfo.setUPDATEORG(SPUtil.getStrjddwno(PersonalFileActivity.this));
			}
			currentFragment.save();
		} else if (id == R.id.btn_back) {
			finish();
		} else {
		}

	}

	public interface onFragmentSave {
		void save();

		/**
		 * 体重
		 * @param value
		 */
		void setTizhong(String value);

		/**
		 * 血糖
		 * @param value
		 */
		void setXuetang(String value);

		/**
		 * 血压
		 * @param gaoya 高压
		 * @param diya 低压
		 * @param mailv 脉率
		 */
		void setXueya(String gaoya, String diya, String mailv);

		/**
		 * 体温
		 * @param tiwen
		 */
		void setTiWen(String tiwen);

		/**
		 * 血红蛋白
		 * @param xhdb
		 */
		void setXueHongDanBai(String xhdb);

		/**
		 * 脉率
		 * @param mailv
		 */
		void setMaiLv(String mailv);

		/**
		 * 心率
		 * @param xinlv
		 */
		void setXinLv(String xinlv);
		/**
		 * 血氧
		 * @param xueYang
		 */
		void setXueYang(String xueYang);


		/**
		 * 理邦心电
		 * @param libangEcgResult
		 * @param time
		 */
		void setLiBangECG(String libangEcgResult, String time);

		/**
		 *
		 * 尿常规
		 * @param niaoDanYuan
		 * @param qianXue
		 * @param DanHongSu
		 * @param tongti
		 * @param puTaoTang
		 * @param danBaiZhi
		 * @param PH
		 * @param yaXiaoSuan
		 * @param baiXiBao
		 * @param biZhong
		 * @param weiShengSu
		 */
		void setNiaoYeFenxi(String niaoDanYuan, String qianXue, String DanHongSu, String tongti, String puTaoTang,
				String danBaiZhi, String PH, String yaXiaoSuan, String baiXiBao, String biZhong, String weiShengSu);

		/**
		 * 总胆固醇
		 * @param dgc
		 */
		void setCHOL(String dgc);
	}

	public interface onFragmentFresh {
		void setData();
	}

	public interface onFragmentUpData {
		void updateSfData(int id);
	}

	public void freshDetailInfoData() {
		onFragmentFresh fresh = (onFragmentFresh) detailFragment;
		fresh.setData();

	}

	@SuppressLint("ResourceAsColor")
	private void setChecked(int pos) {

		if (pos == 0) {
			title.setText("个人档案  ——  封面信息");
		} else if (pos == 1) {
			title.setText("个人档案  ——  详细信息");
		} else if (pos == 2) {
			title.setText("个人档案  ——  健康信息");
		}
	}

}
